package com.cq.androidtools.recycleview

import android.annotation.SuppressLint
import android.util.Log
import android.view.MotionEvent
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.cq.androidtools.databinding.GridRecycleviewBinding
import com.cq.androidtools.jetpack.databinding.DataBindingActivity
import com.cq.androidtools.recycleview.table.CommonAdapter
import com.cq.androidtools.recycleview2.AdapterTop
import com.cq.base.utils.isTouchPointInView


/**
 * 作者: CQ
 * 日期: 2021-03-16
 * 说明: 实例
 */
class Recycleview2Activity : DataBindingActivity<GridRecycleviewBinding>() {

    var isIntoTop = false // 是否是顶部recycleView移动
    var isIntoContent = false // 是否是中间recycleView移动
    var isIntoLeft = false // 是否是左边recycleVIew移动

    @SuppressLint("NewApi")
    override fun GridRecycleviewBinding.initBinding() {
        val listLeft = mutableListOf<String>()
        for (i in 1..5) {
            listLeft.add(i.toString())
        }
        val linearLayoutManager1 = LinearLayoutManager(baseContext)
        linearLayoutManager1.orientation = RecyclerView.VERTICAL
        mBinding.recyclerViewLeft.layoutManager = linearLayoutManager1
        val adapterLeft = CommonAdapter()
        mBinding.recyclerViewLeft.adapter = adapterLeft
        mBinding.recyclerViewLeft.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                if (!isIntoTop && isIntoLeft && !isIntoContent) {
                    Log.e("recyclerViewLeft", "onScrolled：dx = $dx； dy = $dy")
                    mBinding.nestedScrollView.smoothScrollBy(dx, dy)
                }
            }
        })

        val listTop =
            mutableListOf("A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "O", "P", "Q", "R")
        val linearLayoutManager = LinearLayoutManager(baseContext)
        linearLayoutManager.orientation = RecyclerView.HORIZONTAL
        mBinding.recyclerViewTop.layoutManager = linearLayoutManager
        val adapterTop = AdapterTop(listTop, baseContext)
        mBinding.recyclerViewTop.adapter = adapterTop

        mBinding.recyclerViewTop.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                if (isIntoTop && !isIntoLeft && !isIntoContent) {
                    Log.e("recyclerViewTop", "onScrolled：dx = $dx； dy = $dy")
                    mBinding.recyclerViewContent.scrollBy(dx, dy)
                }
            }
        })


        val listContext = mutableListOf<String>()
        listTop.forEach {
            for (i in 0..5) {
                listContext.add("$it$i")
            }
        }
        val gridLayoutManager = GridLayoutManager(baseContext, 30)
        gridLayoutManager.orientation = RecyclerView.HORIZONTAL
        mBinding.recyclerViewContent.layoutManager = gridLayoutManager
        val adapterContext = AdapterTop(listContext, baseContext)
        mBinding.recyclerViewContent.adapter = adapterContext


        mBinding.recyclerViewContent.addOnScrollListener(object : RecyclerView.OnScrollListener() {

            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                if (!isIntoTop && !isIntoLeft && isIntoContent) {
                    Log.e("recyclerViewContent", "onScrolled：dx = $dx； dy = $dy")
                    mBinding.recyclerViewTop.scrollBy(dx, dy)
                }
            }
        })


        mBinding.nestedScrollView.setOnScrollChangeListener { _, scrollX, scrollY, oldScrollX, oldScrollY ->
            Log.e("nestedScrollView", "scrollX = $scrollX； scrollY = $scrollY； oldScrollX = $oldScrollX； oldScrollY = $oldScrollY")
            if (!isIntoTop && !isIntoLeft && isIntoContent) {
                val dx = scrollX - oldScrollX
                val dy = scrollY - oldScrollY
                mBinding.recyclerViewLeft.scrollBy(dx, dy)
            }
        }
    }


    override fun dispatchTouchEvent(event: MotionEvent?): Boolean {
        when (event!!.action) {
            MotionEvent.ACTION_DOWN -> {
                isIntoTop =
                    mBinding.recyclerViewTop.isTouchPointInView(event.rawX.toInt(), event.rawY.toInt())
                isIntoLeft =
                    mBinding.recyclerViewLeft.isTouchPointInView(event.rawX.toInt(), event.rawY.toInt())
                isIntoContent =
                    mBinding.recyclerViewContent.isTouchPointInView(event.rawX.toInt(), event.rawY.toInt())
                if (isIntoTop || isIntoLeft) {
                    isIntoContent = false
                }
                Log.e("ACTION_DOWN", "isIntoTop = $isIntoTop；isIntoLeft = $isIntoLeft；isIntoContent = $isIntoContent ")
            }
        }
        return super.dispatchTouchEvent(event)
    }


}

